package paf.ui;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import paf.exporter.ExportFacade;
import paf.exporter.FileInterface;
import paf.importerandtranslators.FileStrategy;

public class MenuBarUI extends JMenuBar {

	private UiController uiController;
	
	private JMenu jmnFile, jmnFileImport, jmnFileExport;
	private JMenuItem jmiSave, jmiOpen, jmiExportJPG, jmiExportPDF;
	private ExportFacade ex;
	
	protected MenuBarUI(UiController uiController) {
		super();
		ex = new ExportFacade();
		this.uiController = uiController;
		
		this.add(getFileMenu());
	}
	
	private JMenu getFileMenu() {
		jmnFile = new JMenu("File");
		jmnFile.setMnemonic(KeyEvent.VK_F);
		
		jmiSave = new JMenuItem("Save", KeyEvent.VK_S);
		jmiSave.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		jmiSave.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				
			}
		});
		jmnFile.add(jmiSave);
		
		jmiOpen = new JMenuItem("Open", KeyEvent.VK_O);
		jmiOpen.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		jmiOpen.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				
			}
		});
		jmnFile.add(jmiOpen);
		
		jmnFile.add(getFileImportMenu());
		jmnFile.add(getFileExportMenu());
		
		return jmnFile;
	}
	
		private JMenu getFileImportMenu() {
			jmnFileImport = new JMenu("Import");
			jmnFileImport.setMnemonic(KeyEvent.VK_I);
			
			for(final FileStrategy fS : uiController.getFileTypes()) {
				JMenuItem jmiImportItem = new JMenuItem(fS.toString());
				jmiImportItem.addMouseListener(new MouseAdapter() {
					@Override
					public void mousePressed(MouseEvent e) {
						uiController.setPage(new FileImportUI(uiController, fS));
					}
				});
				jmnFileImport.add(jmiImportItem);
			}
			
			return jmnFileImport;
		}
		
		/**
		 * getFileExportMenu returns a JMenu.
		 * @return Export Menu Item with all export types, which it gets from the ExportFactory (through ExportFacade).
		 */
		private JMenu getFileExportMenu() {
			jmnFileExport = new JMenu("Export");
			jmnFileExport.setMnemonic(KeyEvent.VK_E);

			for(final FileInterface fI : uiController.getFileInterfaces()) {
				JMenuItem jmiImportItem = new JMenuItem(fI.toString());
				jmiImportItem.addMouseListener(new MouseAdapter() {
					@Override
					public void mousePressed(MouseEvent e) {
						JFileChooser chooser = new JFileChooser();
						chooser.setCurrentDirectory( new File( "./") );
						int actionDialog = chooser.showSaveDialog(MenuBarUI.this);
						if ( actionDialog == JFileChooser.APPROVE_OPTION )
						{
							File fileName = new File( chooser.getSelectedFile( ) + "" );
							if(fileName == null)
								return;
							if(fileName.exists())
							{
								actionDialog = JOptionPane.showConfirmDialog(MenuBarUI.this, "Replace existing file?");
								// may need to check for cancel option as well
								if (actionDialog == JOptionPane.NO_OPTION)
									return;
							}
							fI.writeToFile(MatrixView.get(), fileName.getAbsolutePath());
						}
					}
				});
				jmnFileExport.add(jmiImportItem);
			}

			return jmnFileExport;
		}
}
